# coding=utf-8
'''
@LastEditors  : FlyingHu
@Date         : 2020-05-22 07:43:00
@LastEditTime : 2020-05-22 09:45:49
@Description  : 单词爬虫
'''
import requests
import httpx
import re
from os import path
from lxml import etree
import random
from pprint import pprint as print


def word_of_bing(word: str):
    '''
    @Description: 必应单词爬取
    @Args: word 需要爬取信息的单词
    @Returns: 信息字典,  至少包含意思, 读音, 例句
    '''    
    if not word:
        raise ValueError("word is None")
    base_url = "https://cn.bing.com/dict/search?q={}"  # q=要查询的单词
    # 构造请求url
    url = base_url.format(word)

    # 获取response
    response = requests.get(url, verify=False)
    # 构造html解析
    html = etree.HTML(response.text)
    # 获取单词意思 //div[@class="qdef"]/ul/li//text()  有多个, 提取
    word_means_lis = html.xpath('//div[@class="qdef"]/ul/li')
    word_mean_list = []
    for word_means_li in word_means_lis:
        word_mean_list.append("    ".join(word_means_li.xpath(".//text()")))
    
    # 获取发音 //div[@class="hd_tf"]/a/@onclick， 获取两个， 第一个为美音， 第二个为英音
    sound_dict = {}
    raw_sound_urls = html.xpath('//div[@class="hd_tf"]/a/@onclick')
    for index, raw_sound_url in enumerate(raw_sound_urls):
        result = re.search(r"https?://[\d\w/.]*(?<!')", raw_sound_url)
        if result:
            sound_url = result.group()
            save_path = download_sound(sound_url, f"{word}_{'us' if index == 1 else 'uk'}.mp3")
            if save_path:
                sound_dict['us' if index == 1 else 'uk'] = save_path
        
    # 获取例句 //div[@class="se_li1"]
    ex_sen_divs = html.xpath('//div[@class="se_li1"]')
    ex_sen_list = []
    for ex_sen_div in random.sample(ex_sen_divs, 3):  # 随机取三个例句
        en = "".join(ex_sen_div.xpath('./div[@class="sen_en b_regtxt"]//text()'))  # 英文
        cn = "".join(ex_sen_div.xpath('./div[@class="sen_cn b_regtxt"]//text()'))  # 中文
        ex_sen_list.append({
            "en": en,
            "cn": cn
        })
    
    return {
        "mean": word_mean_list,
        "ex_sen": ex_sen_list,
        "sound": sound_dict
    }

    # 返回示例
    """
    {'ex_sen': [{'cn': '即使球爱撞墙，虽然看上去有点无聊，但只要球能坚持一下下，在彼此的球拍上来回跑几趟，外婆就会笑。',
                'en': 'Even though the shuttlecock loved to touch the wall, as '
                    'long as it could keep on flying in the air for a while, '
                    'grandmother would be smile.'},
            {'cn': '飞行可以让你在任务中迅速返回前一个地点，拜访朋友，光顾商店，银行，等等。',
                'en': 'Flying allows you to quickly return to previous locations '
                    'to turn in quests, meet up with friends, shop, visit the '
                    'bank, and more.'},
            {'cn': '新浪娱乐讯前段时间，“张柏芝再度怀孕”传闻甚嚣尘上，但一直没有得到当事人的回应。',
                'en': 'Some time ago, "Cecilia Cheung pregnant again" rumors were '
                    "flying around, but has not been a party's response."}],
    'mean': ['n.    飞行；(乘)坐飞机；指驾驶飞机',
            'adj.    快速的；会飞的；飞行中的；跃起的',
            'v.    fly的现在分词',
            '网络    飞翔；飞扬；飞行的'],
    'sound': {'uk': './sounds/flying_uk.mp3', 'us': './sounds/flying_us.mp3'}}
    """

    
    


def download_sound(sound_url: str, file_name: str, save_path="./sounds/") -> str:
    '''
    @Description: 下载发音到本地
    @Args: sound_url 发音url
            save_path 要保存的地址
    @Returns: 保存后的路径
    '''
    if not all([sound_url, file_name]):
        raise ValueError("sound_url or file_name is missing")
    response = requests.get(sound_url)
    if response.status_code == 200:
        file_path = path.join(save_path, file_name)
        with open(file_path, "wb") as f:
            f.write(response.content)
        return file_path
        

    
if __name__ == "__main__":
    print(word_of_bing("flying"))
